//
//  Basket.m
//  iFlower
//
//  Created by thohd on 8/25/10.
//  Copyright 2010 iNIL.me. All rights reserved.
//

#import "Basket.h"
#import "Product.h"

@implementation Basket

@synthesize products = products_;
@synthesize finalBasketPrice;
@synthesize totalCost;
@synthesize subTotalCost;
@synthesize discount;
@synthesize shippingFee;
- (id) init
{
	if ((self = [super init]) != nil) {
		products_ = [NSMutableArray new];
	}
	return self;
}
- (float) getBasketPrice
{
	float total = 0.0;
	for (Product* product in products_) {
		total+=[product.price floatValue]*product.amount;
	}
	return total;
}
- (void) refreshBasket
{
	[products_ removeAllObjects];
}

- (void)dealloc
{
	[products_ release];
	[super dealloc];
}
- (void) addToBasket:(Product*)p
{
	BOOL found = NO;
	for (Product* product in products_) {
		if ([product.productCode isEqualToString:p.productCode]) {
			product.amount++;
			found=YES;
			break;
		}
	}
	if (!found) {
		p.amount = 1;
		[products_ addObject:p];
	}
}
- (NSInteger) count
{
	NSInteger c = 0;
	for (Product* product in products_) {
		c+=product.amount;
	}
	return c;
}
- (void) removeFromBasket:(Product*)p
{
	for (Product* product in products_) {
		if ([product.productCode isEqualToString:p.productCode]) {
			[products_ removeObject:product];
		}
	}
}
@end
